package com.suke.czx.modules.wxadmin.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suke.czx.aop.Cacheable;
import com.suke.czx.aop.Cacheable.KeyMode;
import com.suke.czx.common.utils.DateUtils;
import com.suke.czx.modules.wx.vo.UserReadDays;
import com.suke.czx.modules.wxadmin.dao.DkReadPlanDao;
import com.suke.czx.modules.wxadmin.dao.DkUserDao;
import com.suke.czx.modules.wxadmin.entity.DkReadPlanEntity;
import com.suke.czx.modules.wxadmin.entity.DkUserEntity;
import com.suke.czx.modules.wxadmin.service.DkReadPlanService;



@Service("dkReadPlanService")
public class DkReadPlanServiceImpl implements DkReadPlanService {
	@Autowired
	private DkReadPlanDao dkReadPlanDao;
	@Autowired
	private DkUserDao dkUserDao;
	
	@Override
	public DkReadPlanEntity queryObject(String id){
		return dkReadPlanDao.queryObject(id);
	}
	
	@Override
	public List<DkReadPlanEntity> queryList(Map<String, Object> map){
		return dkReadPlanDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return dkReadPlanDao.queryTotal(map);
	}
	
	@Override
	public void save(DkReadPlanEntity dkReadPlan){
		dkReadPlanDao.save(dkReadPlan);
	}
	
	@Override
	public void update(DkReadPlanEntity dkReadPlan){
		dkReadPlanDao.update(dkReadPlan);
	}
	
	@Override
	public void delete(String id){
		dkReadPlanDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		dkReadPlanDao.deleteBatch(ids);
	}

	@Override
	public int batchInsertUserReadPlan(List<DkReadPlanEntity> userReadPlanList) {
		return dkReadPlanDao.batchInsertUserReadPlan(userReadPlanList);
	}

	@Override
	public List<DkReadPlanEntity> queryUserReadPlanById(String userId) {
		return dkReadPlanDao.queryUserReadPlanById(userId);
	}

	@Override
	public DkReadPlanEntity queryUserReadPlanByIdAndDate(Map<String, String> parms) {
		return dkReadPlanDao.queryUserReadPlanByIdAndDate(parms);
	}

	@Override
	@Cacheable(keyMode=KeyMode.BASIC,expire=7200)
	public UserReadDays getUserReadDays(String openid) {
		
		//构建打卡页面用户分享数据
		List<DkReadPlanEntity> list = dkReadPlanDao.queryUserReadPlanById(openid);
		int readDays = 0;
		int continuousReadDays = 0; 
		String tomorrowDate = DateUtils.getDate(new Date(),1);
		String taodyDate = DateUtils.getDate(new Date(),1);
		String dates = "";
		for(DkReadPlanEntity urp : list) {
		  dates = urp.getReadDate();
		  if (tomorrowDate.equals(dates)) {
			  break;
		  }
		  if (urp.getStatus() == 1) {
			  readDays++;
			  continuousReadDays++;
		  } else if (urp.getStatus() == 2) {
			  readDays++;
			  continuousReadDays++;
		  } else {
			  if (!taodyDate.equals(dates)) {
				  continuousReadDays = 0;
			  }
			  
		  }
		}
		
		//获取对应的用户信息
		DkUserEntity dkUser = dkUserDao.queryObject(openid);
		UserReadDays userReadDays = new UserReadDays();
		userReadDays.setReadDays(readDays);
		userReadDays.setContinuousReadDays(continuousReadDays);
		userReadDays.setOpenid(openid);
		userReadDays.setNickname(new String(dkUser.getNickname()));
		userReadDays.setHeadimgurl(dkUser.getHeadimg());
		return userReadDays;
	}

	@Override
	public int updateSendStateBatch(Object[] id) {
		return dkReadPlanDao.updateSendStateBatch(id);
	}
	
}
